
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- =============================================
-- Author:		LAXMAN S
-- Create date: 05-05-2012
-- Description:	Creating Customer
-- =============================================
CREATE PROCEDURE [lgw].[usp_Customer_Create] 
(
	-- Add the parameters for the stored procedure here
			@in_UserTypeId bigint
           ,@in_UserName nvarchar(10)
           ,@in_Password nvarchar(30)
           ,@in_CustomerStatusId bigint
           ,@in_UpdatedAccountStatusDate datetime
           ,@in_CreatedUser nvarchar(25)
           ,@in_UpdatedUser nvarchar(50)
           ,@in_ReferenceBy nvarchar(50)
           ,@in_ReferenceCustomerId bigint
           ,@out_CustomerId bigint output
           
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

BEGIN TRY

IF NOT EXISTS (SELECT TOP 1 UserName FROM  lgw.Customers (nolock) WHERE UserName=@in_UserName)
BEGIN

	CREATE TABLE #TempCustomer(CustomerId BIGINT)

    -- Insert statements for procedure here
	INSERT INTO [lgw].[Customers]
           ([UserTypeId]
           ,[UserName]
           ,[Password]
           ,[CustomerStatusId]
           ,[UpdatedAccountStatusDate]
           ,[CreatedUser]
           ,[UpdatedUser]
           ,[ReferenceBy]
           ,[ReferenceCustomerId]) OUTPUT INSERTED.[CustomerId] INTO #TempCustomer
     SELECT @in_UserTypeId
			,@in_UserName
			,@in_Password
			,@in_CustomerStatusId
			,@in_UpdatedAccountStatusDate
			,@in_CreatedUser
			,@in_UpdatedUser
			,@in_ReferenceBy
			,@in_ReferenceCustomerId
	
	IF @@ERROR <> 0
      BEGIN
        RAISERROR(N'Error While Creating Customer', 16, 0);
      END;
		
	SELECT @out_CustomerId=CustomerId FROM #TempCustomer
			
END 
ELSE SET @out_CustomerId=-1

END TRY			
BEGIN CATCH 
	SET @out_CustomerId=-99
END CATCH

END


GO
